const whire = [ '/logins', '/signs' ]
const Jwt = require('jsonwebtoken')
const auth = () => {
    return async (ctx, next) => {
        if (!whire.includes(ctx.path)){
            const { authorization} = ctx.request.header
            if (authorization) {
                try {
                    Jwt.verify(authorization,'0311')
                    await next()
                } catch (err) {
                    console.log(err);
                    ctx.body = {
                        message: 'token验证不通过',
                        code: 400,
                        err
                    }
                }
            } else {
                ctx.body = {
                    message: 'token不存在',
                    code: 400
                }
            }
        } else {
            await next()
        }
    }
}

module.exports = auth